var prefix = ctx +"module/eventsSection/";
var form, table;
$(function() {
    layui.use(['form','table'], function() {
        form = layui.form;
        table = layui.table;
        form.on('select(typeId)', function (data) {
            typeIdChange(data.value,form)
        });
    });
});

$().ready(function() {
    validateRule();
});
function typeIdChange(parentId){
    $.ajax({
        type: "POST",
        url: ctx +"module/eventsType/parentIdChange",
        data: {
            parentId:parentId
        },
        success: function(data){
            if(data.code == 0){
                var eventsTypeList = data.data;
                $("#typeChildId").html("");
                if(eventsTypeList.length > 0){
                    $("#typeChildId").append("<option value=\"\" selected=\"selected\">类型/区域</option>");
                    for(var index in eventsTypeList) {
                        var str = "<option value=\""+eventsTypeList[index].id+"\" >"+eventsTypeList[index].typeName+"</option>";
                        $("#typeChildId").append(str);
                    }
                }else{
                    var str = "<option value=\"\" selected=\"selected\">----无----</option>";
                    $("#typeChildId").append(str);
                }
                form.render("select");
            }
        }
    });
}
$.validator.setDefaults({
    submitHandler : function() {
        update();
    }
});


function update() {
    var url = prefix + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
    $.ajax({
        cache : true,
        type : "POST",
        url : url,
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}


function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            sort : {
                required : true
            }
        },
        messages : {
            sort : {
                required : icon + "请输入索引"
            }
        }
    })
}